var web3 = new Web3();
// var eth_node_url = 'http://152.32.253.207:8545'; // TODO: remote URL
var eth_node_url = 'http://127.0.0.1:8545'; // TODO: remote URL
web3.setProvider(new web3.providers.HttpProvider(eth_node_url));
var eth = web3.eth;
function hex2ascii(hex) {
    var str = '';
    for (var i = 2; (i < hex.length && hex.substr(i, 2) !== '00'); i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}

function getTx(start,maxBlock,cb,rb) {
    eth.getBlockNumber().then(function(blockNum) {
        end = start+maxBlock;
        if (maxBlock+start > blockNum) {
            end = blockNum;
        }
        rb(end)
        processBlock(start,end,cb);
    });
}

function processBlock(bn,end,cb) {
    eth.getBlock(bn).then( function(block) {
        for(var s = 0; s < block.transactions.length; ++s) {
            eth.getTransaction(block.transactions[s])
               .then( function(tx) {
                    if (tx.to != null && tx.to.toLowerCase() == "0x3cac89c5117586a6132b6391cc290e41670be5b6" 
                        && tx.input != null) {
                        cb({
                              "block" : block
                            , "from"  : tx.from
                            , "msg"   : hex2ascii(tx.input)
                        });
                    } 
               })
        }
        if(bn<end) {
            processBlock(bn+1,end,cb);
        }
    });                        
}

// getCoords
function InitMap() {
    var center = new BMap.Point(110.,35.)
    var map = new BMap.Map("map");    // 创建Map实例
    map.centerAndZoom(center, 5);  // 初始化地图,设置中心点坐标和地图级别
    map.enableScrollWheelZoom();
    map.addControl(new BMap.MapTypeControl({
        mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]
    }));
    var Done = new Map();
    var Start = 37200;
    function show(res) {
        var date = new Date(res.block.timestamp*1000);
        var sTime = (date.getFullYear()) + "/" + 
           (date.getMonth() + 1) + "/" +
           (date.getDate()) + " " + 
           (date.getHours()) + ":" + 
           (date.getMinutes()) + ":" + 
           (date.getSeconds());
        sTime = sTime.slice(2)
        start = res.block.number;
        console.log(sTime);
        console.log(start,res.from,res.msg);
        if( !Done.has(res.from) ) {
            var data = res.msg.split(",");

            var pt = new BMap.Point(parseFloat(data[2]),parseFloat(data[3]));
            var marker = new BMap.Marker(pt);
            map.addOverlay(marker);
            var rec = {   Time:sTime
                        , Point:pt
                        , StudentNumber:data[0]
                        }
            // Done.set(res.from, rec);
            var sLable = res.from.slice(2,6)+"..."+res.from.slice(-4) + "</br>" +sTime
            var label = new BMap.Label(rec.StudentNumber+" : "+"</br>"+sLable,{offset:new BMap.Size(20,-10)});
            
            var labelStyle = {
                    color: "#fff",
                    backgroundColor: "#333333",
                    border: "0",
                    fontSize : "14px",
                    width:"120px",
                    opacity:"0.8",
                    verticalAlign:"center",
                    borderRadius: "2px",
                    whiteSpace:"normal",
                    wordWrap:"break-word",
                    padding:"7px",
                    display:"none",
                };

            label.setStyle(labelStyle);
	        marker.setLabel(label);
            function myClickhide() {
                label.setStyle({display:"none"});
            }
            function myClickshow() {
                label.setStyle({display:"block"});
            }
            marker.addEventListener("mouseover", myClickshow);
            marker.addEventListener("onmouseout", myClickhide);
        }
    }
    function setEnd(bn) {
        if( bn > Start ) {
            Start = bn;
            console.log(bn)
        }
        setTimeout(find,5000);
    }
    function find() {
        getTx(Start,1000,show,setEnd);
    }
    find()    
}


